<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.reportNo" clearable placeholder="输入汇报单号" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.expressNumber" clearable placeholder="输入快递单号" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.userTesterId" clearable placeholder="检测师" class="filter-item" filterable style="width: 250px" @change="crud.toQuery">
          <el-option v-for="item in optionsTesterList" 
            :value="item.uid"
            :key="item.uid"
            :label="item.userName + ' - ('+item.nickname+')'">
          </el-option>
        </el-select>
        <el-select v-model="query.status" clearable placeholder="汇报状态" class="filter-item" style="width: 130px" @change="crud.toQuery">
          <el-option
            v-for="item in queryStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <el-button
          type="primary"
          class="filter-item"
          size="mini"
          icon="el-icon-open"
          :loading="statusLoading"
          :disabled="crud.selections.length === 0"
          @click="batchApplyMsgReading"
        >批量处理消息</el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible" title="查看详情" width="70%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" style="margin-top: -4%;">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item title="汇报单详情" name="1">
              <el-descriptions direction="vertical" :column="6" border>
                  <el-descriptions-item label="寄存商品" :span="3">{{mailReport.depositGoods}}</el-descriptions-item>
                  <el-descriptions-item label="汇报单号">{{mailReport.reportNo}}</el-descriptions-item>
                  <el-descriptions-item label="消息">
                    <el-tag size="small" v-if="mailReport.msgReading == 1" style="color: green;">已读</el-tag>
                    <el-tag size="small" v-else style="color: red;">未读</el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="寄存状态">
                    <el-tag size="small" v-if="mailReport.status == 2" style="color: green;">物品寄存</el-tag>
                    <el-tag size="small" v-else-if="mailReport.status == 1" style="color: blue;">物品驳回</el-tag>
                    <el-tag size="small" v-else style="color: red;">待处理</el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="客户姓名">{{mailReport.userName}}</el-descriptions-item>
                  <el-descriptions-item label="客户电话">{{mailReport.userPhone}}</el-descriptions-item>
                  <el-descriptions-item label="客户微信">{{mailReport.userWechat}}</el-descriptions-item>
                  <el-descriptions-item label="寄存方式">
                    <el-tag size="small" v-if="mailReport.depositWays == 1" style="color: red;">当面</el-tag>
                    <el-tag size="small" v-else >顺丰</el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="快递公司">{{mailReport.expressCompany}}</el-descriptions-item>
                  <el-descriptions-item label="快递单号">{{mailReport.expressNumber}}</el-descriptions-item>
                  <el-descriptions-item label="收货地" :span="6">{{mailReport.receiptAddress}}</el-descriptions-item>
                  <el-descriptions-item label="发货地" :span="6">{{mailReport.deliverAddress}}</el-descriptions-item>
                  <el-descriptions-item label="汇报描述" :span="5">{{mailReport.reportDes}}</el-descriptions-item>
                  <el-descriptions-item label="凭证" :contentStyle="{'display': 'flex'}">
                    <div class="demo-image__preview"  v-for="(item, index) in voucherImgArray" :key="index" style="margin: 0px 10px;">
                      <el-image 
                        style="width: 50px; height: 50px"
                        :src="item" 
                        :preview-src-list="voucherImgArray">
                      </el-image>
                    </div>                     
                  </el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
            
            <el-collapse-item title="客服详情" name="1">
              <el-descriptions direction="vertical" :column="4" border>
                  <el-descriptions-item label="客户账号">{{syUserService.userName}}</el-descriptions-item>
                  <el-descriptions-item label="客户姓名">{{syUserService.nickname}}</el-descriptions-item>
                  <el-descriptions-item label="电话">{{syUserService.phone}}</el-descriptions-item>
                  <el-descriptions-item label="微信号"><el-tag size="small">{{syUserService.wechatNumber}}</el-tag></el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
            <el-collapse-item title="检测师详情" name="1">
              <el-descriptions direction="vertical" :column="4" border>
                  <el-descriptions-item label="客户账号">{{syUserTester.userName}}</el-descriptions-item>
                  <el-descriptions-item label="客户姓名">{{syUserTester.nickname}}</el-descriptions-item>
                  <el-descriptions-item label="电话">{{syUserTester.phone}}</el-descriptions-item>
                  <el-descriptions-item label="微信号"><el-tag size="small">{{syUserTester.wechatNumber}}</el-tag></el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
          </el-collapse>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="dialogVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column show-overflow-tooltip v-if="columns.visible('reportNo')" prop="reportNo" label="汇报单号" width="150"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('depositGoods')" prop="depositGoods" label="寄存商品"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('receiptAddress')" prop="receiptAddress" label="省市区" width="80"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('deliverAddress')" prop="deliverAddress" label="发货地" width="80"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('userName')" prop="userName" label="客户姓名" width="80"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('userPhone')" prop="userPhone" label="客户电话" width="100"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('userWechat')" prop="userWechat" label="客户微信号" width="100"/>
        <el-table-column v-if="columns.visible('depositWays')" prop="depositWays" label="寄存方式" width="80">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.depositWays == 0" :type="''">顺丰</el-tag>
              <el-tag v-else :type=" 'info' ">当面</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('msgReading')" prop="msgReading" label="消息" width="80">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.msgReading == 1" :type="''">1-已读</el-tag>
              <el-tag v-else :type=" 'danger' ">0-未读</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('status')" prop="status" label="汇报状态" width="80">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.status == 1" :type="''">1-物品驳回</el-tag>
              <el-tag v-else-if="scope.row.status == 2" :type="''">2-物品寄存</el-tag>
              <el-tag v-else :type=" 'danger' ">0-待处理</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip v-if="columns.visible('createTime')" prop="createTime" label="汇报时间" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="toDetailViews(scope.row.id)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyMailReport from '@/api/syMailReport'
import {getTesterList , getMailInfo, batchMsgReading} from '@/api/syMailReport'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: 'SyMailReportController', url: 'api/syMailReport', sort: 'id,desc', crudMethod: { ...crudSyMailReport }})
const defaultForm = { id: null, userServiceId: null, userTesterId: null, reportNo: null, depositGoods: null, receiptAddress: null, deliverAddress: null, userName: null, userPhone: null, userWechat: null, depositWays: null, expressCompany: null, expressNumber: null, reportDes: null, voucherImg: null, msgReading: null, status: null, createTime: null, handleOpinion: null, updateTime: null, isDel: null }
export default {
  name: 'SyMailReport',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      voucherImgArray:[],
      mailReport:{},
      syUserService:{},
      syUserTester:{},
      optionsTesterList: [],//检测师列表
      activeNames: ['1'],
      statusLoading: false,
      dialogVisible: false,
      permission: {
        add: ['admin', 'syMailReport:add'],
        edit: ['admin', 'syMailReport:edit'],
        del: ['admin', 'syMailReport:del']
      },
      rules: {
        reportNo: [
          { required: true, message: '汇报单号不能为空', trigger: 'blur' }
        ],
        depositGoods: [
          { required: true, message: '寄存商品不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '客户姓名不能为空', trigger: 'blur' }
        ],
        userPhone: [
          { required: true, message: '客户电话不能为空', trigger: 'blur' }
        ],
        depositWays: [
          { required: true, message: '寄存方式：0：顺丰快递；1:当面不能为空', trigger: 'blur' }
        ],
        msgReading: [
          { required: true, message: '消息：0未读 1已读不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '汇报状态（0待处理、1物品驳回、2物品寄存）不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '汇报时间不能为空', trigger: 'blur' }
        ]
      },
      queryStatusOptions: [
        { value: '0', label: '待处理' },
        { value: '1', label: '物品驳回' },
        { value: '2', label: '物品寄存' }
      ],
      queryTypeOptions: [
        { key: 'userServiceId', display_name: '客服id' },
        { key: 'userTesterId', display_name: '检测师id' },
        { key: 'reportNo', display_name: '汇报单号' },
        { key: 'status', display_name: '汇报状态（0待处理、1物品驳回、2物品寄存）' }
      ]
    }
  },
  watch: {
  },
  created() {
    let that = this;
    setTimeout(function() {
      that.getTesterListInit();
    }, 500);
  },
  methods: {
    getTesterListInit(){
      getTesterList().then(res => {
        this.optionsTesterList = res.testerList;
      })
    },
    handleChange(val) {
      // console.log(val);
    },
    batchApplyMsgReading() {
      const ids = []
      this.crud.selections.forEach(val => {
        ids.push(val.id)
      })
      this.statusLoading = true
      batchMsgReading(ids).then(() => {
        this.crud.refresh()
        this.crud.notify('更新成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.statusLoading = false
      }).then(() => {
        this.statusLoading = false
      })
    },
    toDetailViews(id) {
      getMailInfo(id).then((res) => {
        this.voucherImgArray = [];
        this.mailReport = res.mailReport;
        let overdueVoucherArray = [];//商品凭证
        if(this.mailReport.voucherImg){
          overdueVoucherArray = JSON.parse(this.mailReport.voucherImg);
        }
        if(overdueVoucherArray.length > 0){
          overdueVoucherArray.forEach(item => {
            this.voucherImgArray.push(item.imgUrl);
          })
        }else{
          this.voucherImgArray = [];
        }
        this.syUserService = res.syUserService;
        this.syUserTester = res.syUserTester;
        this.dialogVisible = true
      }).then(() => {
        this.dialogVisible = true
      })
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.userServiceId
        delete this.crud.params.userTesterId
        delete this.crud.params.reportNo
        delete this.crud.params.status
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
 
</style>
